package com.neuedu.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.Device;
import com.neuedu.model.DeviceType;
import com.neuedu.model.Provider;
import com.neuedu.service.DeviceService;

@Controller
@RequestMapping("/admin/device")
public class DeviceAction {

	@Autowired
	private DeviceService DeviceService;

	@RequestMapping(value = "/findAll")
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("startPage", (page - 1) * rows);
		pageMap.put("endPage", rows);

		List<Device> u = DeviceService.findAll(pageMap);
		
		int total = DeviceService.countDevice();
		map.put("rows", u);
		map.put("total", total);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="addDevice")
	public void addUserMessage(MultipartFile file1,Device device,HttpServletRequest request, HttpServletResponse response) throws Exception {

		// 文件的原始名称
		System.out.println("fileName：" + file1.getOriginalFilename());
		// 取得网站程序在服务器磁盘的路径
		String basePath = request.getServletContext().getRealPath("/");

		// 重新使用时间命名文件名，避免出现中文或不雅文件名
		String newFileName = System.currentTimeMillis()
				+ file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));

		String uploadPath = basePath + "/uploads/" + newFileName;
		System.out.println("newFileName：" + uploadPath);
		try {
			File outputFile = new File(uploadPath);
			device.setImages("uploads/" + newFileName);
			file1.transferTo(outputFile);
		} catch (IOException e) {
			System.out.println("上传文件出错！------");
		}	
		
		// 调用业务逻辑层进行处理，返回数据库表数据更新的行数：更新成功数量大于等于1，更新失败返回0
		int res = DeviceService.addDevice(device);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="updateDevice")
	public void updateUserMessage(MultipartFile file1,Device device, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if(!file1.isEmpty()) {
			// 文件的原始名称
			System.out.println("fileName：" + file1.getOriginalFilename());
			// 取得网站程序在服务器磁盘的路径
			String basePath = request.getServletContext().getRealPath("/");
	
			// 重新使用时间命名文件名，避免出现中文或不雅文件名
			String newFileName = System.currentTimeMillis()
					+ file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
	
			String uploadPath = basePath + "/uploads/" + newFileName;
			System.out.println("newFileName：" + uploadPath);
			try {
				File outputFile = new File(uploadPath);
				device.setImages("uploads/" + newFileName);
				file1.transferTo(outputFile);
			} catch (IOException e) {
				System.out.println("上传文件出错！------");
			}		
		}	
		
		// 调用业务逻辑层进行处理，返回数据库表数据更新的行数：更新成功数量大于等于1，更新失败返回0
		int res = DeviceService.updateDevice(device);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
	
	@RequestMapping(value="deleteDevice")
	public void deleteDevice(Device Device,HttpServletRequest request,HttpServletResponse response) throws Exception{
		int res=DeviceService.deleteDevice(Device.getDeviceNo());
		Map<String, Object> map=new HashMap<String, Object>();
		if(res>0){
			map.put("success", true);
		}else{
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str=JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}

	

}
